<template>
  <div>
    <el-card>
      <CateGorySelect @getCategoryId="getCategoryId" :show="screen > 1"></CateGorySelect>
    </el-card>
    <el-card style="margin-top: 20px">
      <!-- 底部三级联动内容的切换部分 -->
      <div v-show="screen === 1">
        <!-- 展示SPU列表的结构 -->
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSPu">添加SPU</el-button>
        <el-table style="width: 100%; margin-top: 20px" border :data="spuList">
          <el-table-column type="index" :index="aa" label="序号" width="80px" align="center"> </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width"> </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width"> </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template #default="{ row, $index }">
              <HintButton type="success" icon="el-icon-plus" size="mini" title="添加sku" @click="addSku(row, $index)"></HintButton>
              <HintButton type="warning" icon="el-icon-edit" size="mini" title="修改spu" @click="updateSpu(row)"></HintButton>
              <HintButton type="info" icon="el-icon-info" size="mini" title="查看当前spu的全部sku列表" @click="handler(row)"></HintButton>
              <el-popconfirm :title="`确定${row.spuName}删除吗？`" @onConfirm="deleteSpu(row, $index)">
                <HintButton type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference"></HintButton>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit" layout=" prev, pager, next,jumper,->,sizes,total" :total="total" style="text-align: center" @size-change="handleSizeChange" @current-change="handleCurrentChange"> </el-pagination>
      </div>
      <SpuForm v-show="screen === 2" @changeScreen="changeScreen" ref="spu"></SpuForm>
      <SkuForm v-show="screen === 3" @changeScreen="changeScreen1" ref="sku"></SkuForm>
    </el-card>
    <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column label="名称" prop="skuName" width="width"> </el-table-column>
        <el-table-column label="价格" prop="price" width="width"> </el-table-column>
        <el-table-column label="重量" prop="weight" width="width"> </el-table-column>
        <el-table-column label="默认图片" prop="label" width="width">
          <template #default="{ row, $index }">
            <img :src="row.skuDefaultImg" :alt="row.skuName" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { TimeSelect } from 'element-ui'
import SkuForm from './SkuForm/index.vue'
import SpuForm from './SpuForm/inex.vue'
import { done } from 'nprogress'
export default {
  name: 'Spu',
  data() {
    return {
      isShoweTable: true, //控制三级联动的选择
      category1Id: '', //一级商品的id
      category2Id: '',
      category3Id: '',
      page: 1, //当前页码
      limit: 3, //每页的数据
      total: 10, //总页数
      spuList: [], //SPU的列表数据
      screen: 1, //切换展示添加修改spu 1:展示spu的数据,2：添加spu,3：添加sku
      dialogTableVisible: false, //控制查看sku的对话框的显示与隐藏
      spu: [], //存储spu的信息以便于在获取sku
      skuList: [], //存储sku的数据以便于在sku的对话框显示
      loading: true,
    }
  },
  methods: {
    // 获取三级联动的商品id
    getCategoryId(form) {
      this.category1Id = form.category1Id
      this.category2Id = form.category2Id
      this.category3Id = form.category3Id
      if (this.category1Id && this.category2Id && this.category3Id) {
        this.getSpuList()
      }
    },
    //获取Spu列表数据
    async getSpuList() {
      let data = { page: this.page, limit: this.limit, category3Id: this.category3Id }
      let res = await this.$api.spu.reqSpuList(data)
      if (res.code == 200) {
        this.total = res.data.total
        this.spuList = res.data.records
      }
    },
    //点击分页器的处理函数
    handleSizeChange(size) {
      this.limit = size
      this.getSpuList()
    },
    handleCurrentChange(page) {
      this.page = page
      this.getSpuList()
    },
    //添加SPu按钮
    addSPu() {
      this.screen = 2
      //通知子组件获取品牌信息
      this.$refs.spu.addSpuDate(this.category3Id)
    },
    //修改SPU
    updateSpu(row) {
      this.screen = 2
      this.$refs.spu.initSpuData(row) //每一次点击修改spu自动调用SpuForm组件的initSpuData()
    },
    //自定义事件(spuForm的取消按钮)
    changeScreen(data) {
      //父组件修改场景后，更新数据
      if (data.flag === '添加') {
        this.screen = data.screeen
        this.page = 1
        this.getSpuList()
      } else {
        this.screen = data.screeen
        this.getSpuList()
      }
    },
    //删除spu的属性（按钮）
    async deleteSpu(row, index) {
      let res = await this.$api.spu.reqDelteAttr({ spuId: row.id })
      if (res.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        this.page = this.spuList.length > 1 ? this.page : this.page - 1
        this.getSpuList()
      }
    },
    aa(index) {
      return index + (this.page - 1) * this.limit + 1
    },
    // 添加sku（按钮）
    addSku(row) {
      this.screen = 3
      // 调用子组件，获取数据
      this.$refs.sku.getData([this.category1Id, this.category2Id, this.category3Id, row])
    },
    //sku的取消按钮的自定义事件
    changeScreen1(data) {
      this.screen = data
      this.getSpuList()
    },
    // 查看sku的列表按钮
    async handler(row) {
      this.dialogTableVisible = true
      this.spu = row
      // 获取sku的列表数据
      let res = await this.$api.sku.reqSkuList({ spuId: row.id })
      if (res.code == 200) {
        this.skuList = res.data
        this.loading = false
      }
    },
    //关闭sku显示的对话框
    close(done) {
      this.loading = true
      this.skuList = []
      done()
    },
  },
  components: {
    SkuForm,
    SpuForm,
  },
}
</script>

<style lang='less' scoped>
</style>